import Opt from '@/components/dt/info/Opt.js'
import nameOpt from '@/biz/option/name.js'
import ruleOpt from '@/biz/option/rule.js'
import tool from '@/common/tool.js'

class SearchOpt extends Opt {
  constructor(info) {
    super(info)
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    let monthRange = {
      title: '奖金月份',
      placeholder: '请选择',
      key: 'bonusStartMonth',
      dtTime: {
        dateType: 'month',
        dateFormat: 'YYYY-MM',
        clearable: true
      },
      data: {
        from: (info) => {
          if (info.bonusStartMonth || info.bonusEndMonth) {
            return [info.bonusStartMonth, info.bonusEndMonth]
          }
          return null
        },
        to: (val) => {
          return {
            bonusStartMonth: val[0],
            bonusEndMonth: val[1]
          }
        }
      },
      rules: [
        ruleOpt.check(
          '请选择奖金月份范围',
          (val) => {
            return (
              (this.info.bonusEndMonth && !this.info.bonusStartMonth) ||
              (!this.info.bonusEndMonth && this.info.bonusStartMonth)
            )
          },
          'change'
        ),
        ruleOpt.check(
          '月份范围最大选择2年',
          (val) => {
            return (
              this.info.bonusEndMonth &&
              this.info.bonusStartMonth &&
              tool.dateDiff(this.info.bonusStartMonth, this.info.bonusEndMonth).Y >= 2
            )
          },
          'change'
        )
      ]
    }

    this.all = [
      monthRange,
      {
        key: 'replaceType',
        title: '类型',
        placeholder: '全部',
        select: {
          options: [
            { key: '1', value: '我司代发' },
            { key: '0', value: '他司代发' }
          ]
        }
      },
      nameOpt.createPeople({
        key: 'userId',
        validate: 3,
        title: '工号/姓名',
        to: (info) => {
          return {
            userId: info.userId
          }
        }
      })
    ]
  }
}

export default SearchOpt
